package com.runa.monitor.platinterface.mqtt.vo;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.comm.dto.newComm.CheckDataVO;
import com.runa.monitor.comm.dto.newComm.CheckVO;
import com.runa.persistence.DateUtil;
import com.runa.protocol.util.ByteUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Data
public class FourGCheckVO extends FourGVO {
    private String id;
    private String csq;
    private String icc;
    private String imei;
    private String rsrp;
    private String etime;
    private String t1;
    private String t2;
    private String rt2;
    private String sta;
    private String ver;
    private String rtc;
    private String citv1;
    private String uitv1;
    private String citv2;
    private String uitv2;
    private String hot;
    private String bat;
    private String life;


    @SneakyThrows
    public static FourGCheckVO gene(String json) {
        return generate(FourGCheckVO.class, json);
    }


    public String wakeUpCycle(boolean heatingPeriod) {
        return heatingPeriod ? uitv1 : uitv2;
    }


    public String acquisitionCycle(boolean heatingPeriod) {
        return heatingPeriod? citv1 : citv2;
    }

    public CheckVO getCheckVO() {
        CheckVO checkVO = new CheckVO();
        Date rtc = DateUtil.noLineFormatToDate(getRtc());
        checkVO.setReadTime(rtc);
        checkVO.setEquipTime(rtc);
        // 当前温度
        String currentTemp = getRt2();
        checkVO.setReturnTemp(currentTemp);
        checkVO.setStatus(getSta());
        Float bat = convertFloat(getBat()) * 1000;
        checkVO.setBatteryVol(bat);
        checkVO.setSignal(getCsq());
        checkVO.setWakeUpCycle(wakeUpCycle(DateUtil.heatingPeriod(getHot())));
        checkVO.setAcquisitionCycle(acquisitionCycle(DateUtil.heatingPeriod(getHot())));
        byte[] staBytes = ByteUtil.hexStringToByte(getSta());
        if (null != staBytes && staBytes.length == 2) {
            List<String> faultCode = new ArrayList<>();
            if ((staBytes[0] >> 4 & 1) == 1) {
                faultCode.add("管道分离");
            }
            checkVO.setFaultCode(faultCode); // 管道分离
        }

        List<CheckDataVO> checkDataVOList = new ArrayList<>();
        // 历史温度最后一次采集时间
        Date endTime = DateUtil.noLineFormatToDate(etime);
        boolean heatingPeriod = DateUtil.heatingPeriod(getHot());
        Integer acquisitionCycle = Integer.valueOf(acquisitionCycle(heatingPeriod));
        String[] t2Arr = getFromArrayStr(getT2());
        if (null != t2Arr && t2Arr.length > 0) {
            int len = t2Arr.length;
            for (int i = len - 1; i >= 0; i--) {
                CheckDataVO checkDataVO = new CheckDataVO();
                Date date = DateUtil.dateSubtractMinutes(endTime, Long.valueOf((len - i - 1) * acquisitionCycle));
                checkDataVO.setReadTime(date);
                checkDataVO.setEquipTime(date);
                checkDataVO.setReturnTemp(t2Arr[i]);
                checkDataVOList.add(checkDataVO);
            }
        }

        checkVO.setHistoryList(checkDataVOList);

        return checkVO;
    }


    public static void main(String[] args) {
        String s = "func:up,id:25052801,typ:1,nbattach1:0,rxw:6,nbdly:1,rep:1,t2:[24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5_24.5],etime:20250529114101,rt2:25.5,sta:9D80,par:0200,par2:0000,ver:9201,rtc:20250529114132,uitv1:15,uitv2:15,citv1:1,citv2:1,ahead:120,hot:10010401,bat:3.603,life:1079,t1c:0.0,t2c:0.0,reset:7,wktm:1382,wktm2:45,rflag:40,reperrcnt:0,cenreptime:10,compile:2025-05-28T17-45-34";
        FourGCheckVO fourGCheckVO = FourGCheckVO.gene(s);
        CheckVO checkVO = fourGCheckVO.getCheckVO();
        AsynResult asynResult = AsynResult.success();
        asynResult.setTimestamp(new Date().getTime());
        asynResult.setIdentity(null);
        asynResult.setContent(checkVO);
        System.out.println(JSON.toJSONString(asynResult));
    }


}
